package concurrent.c_001_Synchronized;

import util.ThreadUtil;

/**
 * 面试题：模拟银行账户
 * 对业务写方法加锁
 * 对业务读方法不加锁
 * 这样行不行？
 * <p>
 * 容易产生脏读问题（dirtyRead）
 */
public class T07_Account {
    String name;
    double balance;

    public static void main(String[] args) {
        T07_Account a = new T07_Account();
        new Thread(() -> a.set("zhangsan", 100.0)).start();
        ThreadUtil.sleepSeconds(1);
        System.out.println(a.getBalance("zhangsan"));
        ThreadUtil.sleepSeconds(2);
        System.out.println(a.getBalance("zhangsan"));
    }

    public synchronized void set(String name, double balance) {
        this.name = name;
        ThreadUtil.sleepMillis(2000);
        this.balance = balance;
    }

    public /*synchronized*/ double getBalance(String name) {
        return this.balance;
    }
}